#include"../include/lib_headers.h"
char* move_dir(char *source, char *destination) {
//	char *output_message=(char*)malloc(sizeof(char)*SIZE);
	int i = 0, j = 0, k = 0, match = 0, t = 0;
	char *s, input[SIZE] = "", *split_char, *p[SIZE], copy[SIZE] = "", dest[SIZE] = "",*s1,var[SIZE]="";
	node *temp, *prev, *current, *source1, *destination1, *child;

	/* VFS Not Mounted */
	if (h == NULL || l1 == NULL) {
		sprintf(output_message,"%s %s","movedir_FAILURE",ERR_VFS_MOVEDIR_08);	
		return output_message;
	}
	
	/* Insufficient Arguments */
	if(strlen(source) == 0 || strlen(destination) == 0 ){
		sprintf(output_message,"%s %s","movedir_FAILURE",ERR_VFS_MOVEDIR_00);	
		return output_message;
	}

	/* Both source and Destination happen to be the same */
	if(strcmp(source, destination) == 0){
		sprintf(output_message,"%s %s","movedir_FAILURE",ERR_VFS_MOVEDIR_05);	
		return output_message;

	}
	
	/* Source directory does not exist */
	if(find_node(l1->next,source)==NULL || source[0] != '/' ){
		sprintf(output_message,"%s %s","movedir_FAILURE",ERR_VFS_MOVEDIR_01);	
		return output_message;
	}
	
	/* Can't find Destination Directory */	
	if(find_node(l1->next,destination)==NULL  || destination[0] != '/' ){
		sprintf(output_message,"%s %s","movedir_FAILURE",ERR_VFS_MOVEDIR_02);	
		return output_message;
	}	

	/* Source can't be a file */
	if(strcmp(find_node(l1->next,source)->fd_pointer->fileType, "dir") != 0){
		sprintf(output_message,"%s %s","movedir_FAILURE",ERR_VFS_MOVEDIR_01);	
		return output_message;
	}
	
	/* Destination can't be a file */
	if(strcmp(find_node(l1->next,destination)->fd_pointer->fileType, "dir") != 0){
		sprintf(output_message,"%s %s","movedir_FAILURE",ERR_VFS_MOVEDIR_07);	
		return output_message;
	}
	if(strcmp(source,"/")==0 ){
		sprintf(output_message,"%s %s","movedir_FAILURE",ERR_VFS_MOVEDIR_06);	
		return output_message;
	}
	
	source1=find_node(l1->next,source); destination1=find_node(l1->next,destination);
	/*Cannot move parent to child directory */
	
	if(strncmp(source1->fd_pointer->fullPath,destination1->fd_pointer->fullPath,
		strlen(source1->fd_pointer->fullPath))==0&&destination1->fd_pointer->fullPath
			[strlen(source1->fd_pointer->fullPath)]=='/')
	{
		sprintf(output_message,"%s %s","movedir_FAILURE",ERR_VFS_MOVEDIR_06);	
			return output_message;
	}
	
	source1=find_node(l1->next,source); destination1=find_node(l1->next,destination);
	if(strcmp(destination, "/") != 0)	
		child=destination1->child;
	else
		child = destination1;	
	
	/* Destination already having the source directory */
	if(child != NULL){
		while(child != NULL){
			if(strcmp(child->fd_pointer->fileName, source1->fd_pointer->fileName) == 0){
				sprintf(output_message,"%s %s","movedir_FAILURE",ERR_VFS_MOVEDIR_05);	
				return output_message;
			}
			
			child=child->sibling;
		}
	}

	split_char = "/";
	s = add_slash(source);

	strcpy(input, s);
	i = splits_directory(input, p, split_char);
	char deleted_text[SIZE] = "";
	node *deleted;
	temp = l1->next;
	prev = temp;
	match = 0;
	j = 0;
	
	while (j < i) {
		while (temp != NULL) {
			if (strcmp(temp->fd_pointer->fileName, p[j]) == 0) {
				match = 1;
				j++;
				if (j == i) {
					break;
				}
				prev = temp;
				temp = temp->child;
			}
			if (match == 0) {
				prev = temp;
				temp = temp->sibling;
			} else if (match == 1) {
				match = 0;
				continue;
			}
		}
	}

	if (prev->child == temp) {
		strcpy(deleted_text, prev->child->fd_pointer->fileName);
		deleted = prev->child;
		prev->child = temp->sibling;
		del(temp->fd_pointer->fullPath);				
		htable_delete(hashtable,*(temp->fd_pointer));
	} else if (prev->sibling == temp) {
		strcpy(deleted_text, prev->sibling->fd_pointer->fileName);
		deleted = prev->sibling;
		prev->sibling = prev->sibling->sibling;
		del(temp->fd_pointer->fullPath);		
		htable_delete(hashtable,*(temp->fd_pointer));
	} else if (prev == l1->next) {
		strcpy(deleted_text, prev->fd_pointer->fileName);
		deleted = prev;
		temp = deleted;
		l1->next = prev->sibling;
		del(prev->fd_pointer->fullPath);		
		htable_delete(hashtable,*(prev->fd_pointer));
	}
		
		deleted->sibling = NULL;
		s1 = add_slash(destination);
		strcpy(dest, s1);
		current = temp;
		strcpy(var,destination);

		if(strcmp(destination,"/") != 0)
			strcat(var, "/"); 

		strcat(var, current->fd_pointer->fileName);
		strcpy(current->fd_pointer->fullPath, var);
		
		append_traversal(deleted, deleted->child, current->fd_pointer->fullPath, dest);
		match = 0;
		j = 0;
		temp = l1->next;
		prev = temp;
		split_char = "/";
		i = splits_directory(dest, p, split_char);
		while (j < i) {
			while (temp != NULL) {
				
				if (strcmp(temp->fd_pointer->fileName, p[j]) == 0) {
					match = 1;
					j++;
					if (j == i) {
						break;
					}
					prev = temp;
					temp = temp->child;
					
				}
				if (match == 0) {
					prev = temp;
					temp = temp->sibling;
				} else if (match == 1) {
					match = 0;
					continue;
				}
			}
		}
		/* if(temp == NULL){
			l1->next = current;
		} 
		*/
		if(i == 0){
			l1->next = current;
			current->sibling = temp;
		}	
		else {
			if (temp->child == NULL){
				temp->child = current;
			}	
			else {
				node *hold = temp->child;
				while (hold->sibling != NULL)
					hold = hold->sibling;
				hold->sibling = current;
			}
		}
	insert(current->fd_pointer);	
	htable_insert(hashtable,*(current->fd_pointer));
	sprintf(output_message,"%s","movedir_SUCCESS");	
	return output_message;
}


